<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sort a result from a dbx_query by a custom sort function</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.dbx-sort" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dbx_sort</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5 &lt; 5.1.0, PECL dbx &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">dbx_sort</span> &mdash; <span class="dc-title">
   Sort a result from a dbx_query by a custom sort function
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.dbx-sort-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dbx_sort</strong></span>
    ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$result</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$user_compare_function</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Sort a result from a <span class="function"><a href="dbx_query.html" class="function">dbx_query()</a></span> call with a custom sort
   function.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.dbx-sort-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">result</code></dt>

     <dd>

      <p class="para">
       A result set returned by <span class="function"><a href="dbx_query.html" class="function">dbx_query()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">user_compare_function</code></dt>

     <dd>

      <p class="para">
       The user-defined comparison function. It must accept two arguments and
       return an integer less than, equal to, or greater than zero if the 
       first argument is considered to be respectively less than, equal to, 
       or greater than the second.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.dbx-sort-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.dbx-sort-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-979">
    <p><strong>Example #1 <span class="function"><strong>dbx_sort()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">user_re_order</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"parentid"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DBX_CMP_DESC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$rv</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DBX_CMP_NUMBER</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$rv</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$link&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">DBX_ODBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;parentid,&nbsp;description&nbsp;FROM&nbsp;tbl&nbsp;ORDER&nbsp;BY&nbsp;id"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;in&nbsp;$result&nbsp;is&nbsp;now&nbsp;ordered&nbsp;by&nbsp;id<br /><br /></span><span style="color: #0000BB">dbx_sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_re_order"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;in&nbsp;$result&nbsp;is&nbsp;now&nbsp;ordered&nbsp;by&nbsp;parentid&nbsp;(descending),&nbsp;then&nbsp;by&nbsp;id<br /><br /></span><span style="color: #0000BB">dbx_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.dbx-sort-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is always better to use <em>ORDER BY</em> 
    <em>SQL</em> clause instead of <span class="function"><strong>dbx_sort()</strong></span>
    if possible.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.dbx-sort-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="dbx_compare.html" class="function" rel="rdfs-seeAlso">dbx_compare()</a> - Compare two rows for sorting purposes</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>